package com.ykdl.member.kid.marketcard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oauth.signpost.http.HttpParameters;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.Card_idBean;
import com.ykdl.member.kid.beans.Card_idListBean;
import com.ykdl.member.kid.beans.DoctorTagBean;
import com.ykdl.member.kid.beans.StoreBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.marketcard.NearbyCardActivity.CarLlistAdapter.ViewHolder;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class MyCardActivity extends BaseScreen{
	private RefreshListView card_list;
	private CarLlistAdapter adapter;
	private BitmapUtils bitmapUtil ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("我的门店卡", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		setContent(R.layout.mycardactivity_xml);
		inint();
	}
	public void inint() {
		findViewById(R.id.add_card).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				go(NearbyCardActivity.class);
			}
		});
		bitmapUtil = new BitmapUtils(this);
		bitmapUtil.configDefaultLoadingImage(R.drawable.test_bg);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.test_bg);
		card_list = (RefreshListView)findViewById(R.id.card_list);
		card_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Card_idBean bean= (Card_idBean)adapter.getItem(arg2);
				Intent intent = new Intent(MyCardActivity.this,CardInfoActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("store_id", bean.getCard_id()+"");
				intent.putExtra("from","false");
				MyCardActivity.this.startActivity(intent);
			}
		});
		adapter = new CarLlistAdapter();
		getData();
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getData();
	}
	public void getData() {
		showProgress("正在加载中,请稍等...");
		String  pageurl =KidConfig.GET_MYCARD;
		HttpParameters parameterss = new HttpParameters();
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,parameterss,null);
		 TaskManager.startHttpRequest(request, new getDataTag(),  Card_idListBean.class);
	}
	
	class getDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof Card_idListBean) {
				Card_idListBean bean = (Card_idListBean)t;
				if (null!=bean.getCard_ids()) {
					adapter.setList(bean.getCard_ids());
					card_list.setAdapter(adapter);
					adapter.notifyDataSetChanged();
					
					
					if(bean.getCard_ids().size()>0){
						hidErrorTip( );
					}else {
						showErrorTip("没有数据...", "");
					}
					card_list.setMoreButtoIsGon(true);
				}else {
					showErrorTip("没有数据...", "");
				}
				
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	class CarLlistAdapter extends BaseAdapter {
		ArrayList<Card_idBean> list;
		public void setList(ArrayList<Card_idBean> list) {
			this.list =list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (list != null) {
				return list.size();
			} else {
				return 0;
			}
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			if (getCount() != 0) {
				return list.get(position-1);
			} else {
				return null;
			}
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = RelativeLayout.inflate(mContext,
						R.layout.mycarllist_item, null);
				holder.card_name = (TextView) convertView.findViewById(R.id.card_name);
//				holder.card_id = (TextView) convertView.findViewById(R.id.card_id);
				holder.card_bg = (ImageView) convertView.findViewById(R.id.card_bg);
				convertView.setTag(holder);
				}else {
					holder = (ViewHolder) convertView.getTag();
				}
			holder.card_name.setText(list.get(position).getCard_name());
//			holder.card_id.setText(list.get(position).getCard_id()+"");
			if (null!=list.get(position).getFile()) {
				bitmapUtil.display(holder.card_bg, list.get(position).getFile().getFile_url());	
			}else {
				holder.card_bg.setBackgroundResource(R.drawable.test_bg);
			}
			return convertView;
		}
		class ViewHolder {
			TextView card_name,card_id;
			ImageView card_bg ;
			
		}
		
	}
}
